﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EncuentraWEB
{
    public partial class Formulario_web17 : System.Web.UI.Page
    {
        private ArqJsCls _arq;
        private clsCoches _obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            var sUser = (string)Session["User"];
            if (sUser != "admin")
            {
                Response.Redirect("~\\default.aspx");
                return;
            }
            _arq = new ArqJsCls();
            _obj = new clsCoches();
            if (!this.IsPostBack)
            {
                try
                {
                    var objEmpresa = new clsEmpresas();
                    ddEmpresa.DataTextField = "varDenominacion";
                    ddEmpresa.DataValueField = "intIdEmpresa";
                    var dtEmpresas = objEmpresa.buscar("", "");
                    var drEmpresa = dtEmpresas.NewRow();
                    drEmpresa["intIdEmpresa"] = 0;
                    drEmpresa["varDenominacion"] = "TODOS";
                    dtEmpresas.Rows.Add(drEmpresa);
                    ddEmpresa.DataSource = dtEmpresas;
                    ddEmpresa.DataBind();
                    ddEmpresa.SelectedIndex = ddEmpresa.Items.Count - 1;

                    Buscar();

                    hidIndice.Value = "-1";

                }
                catch (Exception ex)
                {
                }
            }
        }

        protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            Buscar();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Agregar();
        }

        protected void btnImgEditar_Click(object sender, ImageClickEventArgs e)
        {
            Modificar();
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {

        }
        protected void grdEmpresas_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdEmpresas.PageIndex = e.NewPageIndex;
            Buscar();
        }

        protected void ddEmpresa_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void Agregar()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            _arq.Funciones(this, "ShowEditModal('frmamCoche.aspx?id=I&id1=0')");
            ModalPop.TargetControlID = "btnImgAgregar";
            ModalPop.Show();
            ModalPop.TargetControlID = "btnFix";

        }
        private void Buscar()
        {
            int a = 0;
            DataTable dt = _obj.buscar(txtDominio.Text,Convert.ToInt32(ddEmpresa.SelectedValue));
            if (dt!=null)
            {
                int b = dt.Rows.Count;
                a = b %= 10;
            }
            else
            {
                 a = 10;
            }
            
            if (a != 0)
            {
                for (int i = 1; i <= (10 - a); i++)
                {
                    dt.Rows.Add();
                }
            }

            try
            {
                this.grdEmpresas.DataSource = dt;
                this.grdEmpresas.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        private void Modificar()
        {
            if (Convert.ToInt32(hidIndice.Value) >= 0)
            {
                var id = grdEmpresas.Rows[Convert.ToInt32(hidIndice.Value)].Cells[1].Text;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                _arq.Funciones(this, "ShowEditModal('frmamCoche.aspx?id=M&id1=" + id + "')");
                ModalPop.TargetControlID = "btnImgAgregar";
                ModalPop.Show();
                ModalPop.TargetControlID = "btnFix";
            }
            else
            {
                _arq.Msg(this, "No se ha seleccionado ninguna fila");
            }
        }
      
    }
}
